package se.topshelf.golftracker.web.controller.country;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import se.topshelf.golftracker.core.domain.Country;
import se.topshelf.golftracker.core.domain.Player;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.web.validator.BaseEntityValidator;

import java.util.List;

/**
 *
 */
public class CountryValidator extends BaseEntityValidator<Country> {
    static final String REQUIRED_ERROR_CODE = "required";
    private BaseService baseService;

    public CountryValidator(BaseService baseService) {
        super(Country.class);
        this.baseService = baseService;
    }

    @Override
    protected void validateFields(Country entityToCheck, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "code", REQUIRED_ERROR_CODE, "Attribute is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "continent", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
    }

    @Override
    protected BaseService getBaseService() {
        return baseService;
    }

    @Override
    protected boolean isNaturalKeyEqual(Country savedEntity, Country entityToCheck) {
        return savedEntity.getName().equals(entityToCheck.getName());
    }

    @Override
    protected void doRejectOfNaturalField(Errors errors, Country entityToCheck) {
        errors.rejectValue("name", "duplicate", "Duplicate country name for country name " + entityToCheck.getName());
    }
}
